package com.ruoyi.human.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.human.domain.HumanClinicalTrial;
import com.ruoyi.human.domain.Participant;
import com.ruoyi.human.domain.TrialDataRecord;
import com.ruoyi.human.service.IHumanClinicalTrialService;
import com.ruoyi.human.service.IParticipantService;
import com.ruoyi.human.service.ITrialDataRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.human.domain.TrialDataRecordHistory;
import com.ruoyi.human.service.ITrialDataRecordHistoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 实验数据记录历史Controller
 * 
 * @author hdh
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/recordhistory")
public class TrialDataRecordHistoryController extends BaseController
{
    @Autowired
    private ITrialDataRecordHistoryService trialDataRecordHistoryService;
    @Autowired
    private IHumanClinicalTrialService humanClinicalTrialService;
    @Autowired
    private IParticipantService participantService;
    @Autowired
    private ITrialDataRecordService trialDataRecordService;
    /**
     * 查询实验数据记录历史列表
     */
    @RequiresPermissions("human:recordhistory:list")
    @GetMapping("/list")
    public TableDataInfo list(TrialDataRecordHistory trialDataRecordHistory)
    {
        startPage();
        List<TrialDataRecordHistory> list = trialDataRecordHistoryService.selectTrialDataRecordHistoryList(trialDataRecordHistory);

        return getDataTable(list);
    }

    /**
     * 导出实验数据记录历史列表
     */
    @RequiresPermissions("human:recordhistory:export")
    @Log(title = "实验数据记录历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrialDataRecordHistory trialDataRecordHistory)
    {
        List<TrialDataRecordHistory> list = trialDataRecordHistoryService.selectTrialDataRecordHistoryList(trialDataRecordHistory);
        ExcelUtil<TrialDataRecordHistory> util = new ExcelUtil<TrialDataRecordHistory>(TrialDataRecordHistory.class);
        util.exportExcel(response, list, "实验数据记录历史数据");
    }

    /**
     * 获取实验数据记录历史详细信息
     */
    @RequiresPermissions("human:recordhistory:query")
    @GetMapping(value = "/{historyId}")
    public AjaxResult getInfo(@PathVariable("historyId") Long historyId)
    {
        return success(trialDataRecordHistoryService.selectTrialDataRecordHistoryByHistoryId(historyId));
    }

    /**
     * 新增实验数据记录历史
     */
    @RequiresPermissions("human:recordhistory:add")
    @Log(title = "实验数据记录历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrialDataRecordHistory trialDataRecordHistory)
    {
        return toAjax(trialDataRecordHistoryService.insertTrialDataRecordHistory(trialDataRecordHistory));
    }

    /**
     * 修改实验数据记录历史
     */
    @RequiresPermissions("human:recordhistory:edit")
    @Log(title = "实验数据记录历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrialDataRecordHistory trialDataRecordHistory)
    {
        return toAjax(trialDataRecordHistoryService.updateTrialDataRecordHistory(trialDataRecordHistory));
    }

    /**
     * 删除实验数据记录历史
     */
    @RequiresPermissions("human:recordhistory:remove")
    @Log(title = "实验数据记录历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{historyIds}")
    public AjaxResult remove(@PathVariable Long[] historyIds)
    {
        return toAjax(trialDataRecordHistoryService.deleteTrialDataRecordHistoryByHistoryIds(historyIds));
    }


    @GetMapping(value = "getRecordHistory/{dataId}")
    public AjaxResult getRecordHistory(@PathVariable("dataId") Long dataId)
    {
        List<TrialDataRecordHistory> list = trialDataRecordHistoryService.selectTrialDataRecordHistoryByDataId(dataId);

        for (TrialDataRecordHistory trialDataRecordHistory1:list){
            Long trialId=trialDataRecordHistory1.getTrialId();
            HumanClinicalTrial humanClinicalTrial=humanClinicalTrialService.selectHumanClinicalTrialByTrialId(trialId);
            if(humanClinicalTrial!=null){
                trialDataRecordHistory1.setTrialName(humanClinicalTrial.getTrialName());
            }
        }
        for (TrialDataRecordHistory trialDataRecordHistory1:list){
            Long participantId=trialDataRecordHistory1.getParticipantId();
            Participant participant=participantService.selectParticipantByParticipantId(participantId);
            if(participant!=null){
                trialDataRecordHistory1.setParticipantName(participant.getParticipantName());
            }
        }return success(list);
    }
    @PostMapping(value = "rollback")
    public AjaxResult rollback(@RequestBody TrialDataRecordHistory trialDataRecordHistory)
    {
       TrialDataRecord trialDataRecord=new TrialDataRecord();
       BeanUtils.copyProperties(trialDataRecordHistory,trialDataRecord);
       return toAjax(trialDataRecordService.updateTrialDataRecord(trialDataRecord));
    }
}
